﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Project2
{
    internal class 迭代器
    {
        private static IEnumerable<Int32> CountWithTimeLimit(DateTime limit)
        {
            try
            {
                for (int i = 1; i <= 100; i++)
                {
                    if (DateTime.Now >= limit)
                    {
                        yield break;
                    }
                    yield return i;
                }
            }
            finally
            {
                Console.WriteLine("停止迭代！"); Console.ReadKey();
            }
        }

        private static void Main迭代器(string[] args)
        {
            DateTime stop = DateTime.Now.AddSeconds(2);
            foreach (Int32 i in CountWithTimeLimit(stop))
            {
                Console.WriteLine("返回 {0}", i);
                Thread.Sleep(300);
            }
        }
    }
}